{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 60000 samples\n",
      "Epoch 1/5\n",
      "60000/60000 [==============================] - 4s 63us/sample - loss: 0.4762\n",
      "Epoch 2/5\n",
      "60000/60000 [==============================] - 3s 56us/sample - loss: 0.3613\n",
      "Epoch 3/5\n",
      "60000/60000 [==============================] - 3s 56us/sample - loss: 0.3235\n",
      "Epoch 4/5\n",
      "60000/60000 [==============================] - 3s 57us/sample - loss: 0.2988\n",
      "Epoch 5/5\n",
      "60000/60000 [==============================] - 3s 56us/sample - loss: 0.2786\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tensorflow.python.keras.callbacks.History at 0x16ec1e401d0>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "from tensorflow import keras\n",
    "\n",
    "fashion_mnist = keras.datasets.fashion_mnist\n",
    "(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()\n",
    "\n",
    "# 图像有像素值，这里不需要，因此统一除以255，使图像信息全部转换成0和1的数据(我们只需要外形，并不用根据颜色去区分)\n",
    "train_images = train_images/255\n",
    "test_images = test_images/255\n",
    "model = keras.Sequential([\n",
    "    keras.layers.Flatten(input_shape=(28, 28)), ## 扁平层，将28*28的图像数据转化为一阶数据\n",
    "    keras.layers.Dense(512, activation=tf.nn.relu),  ## 只要最后的神经元个数是10就行，中间隐藏层节点个数可自定义\n",
    "    keras.layers.Dense(10, activation=tf.nn.softmax)\n",
    "])\n",
    "model.compile(optimizer=tf.optimizers.Adam(), loss='sparse_categorical_crossentropy')\n",
    "\n",
    "model.fit(train_images, train_labels, epochs=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10000/10000 [==============================] - 0s 22us/sample - loss: 0.3253\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.32527902796268465"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.evaluate(test_images, test_labels)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
